package gui.threads;

import java.sql.SQLException;

import gui.Main;
import gui.Utils;
import DBside.JDBCConnection.Genres;
import LogicSide.ManageSongs;
import LogicSide.Session;

public class AddSongThread extends Thread {

	private String name;
	private String artist;
	private String album;
	private String specific_genre;
	private String genre;
	private Genres mainGenre;
	
	public AddSongThread(String name, String artist, String album, String genre, String specific_genre) {
		this.name = name;
		this.artist = artist;
		this.album  = album;
		this.genre = genre;
		this.specific_genre = specific_genre;
		for(Genres g : Genres.values()){
			if(g.getName().equals(genre))
				this.mainGenre = g;
		}
	}
	
	@Override
	public void run() {
		try {
			final int result = ManageSongs.addSong(this.name, mainGenre.getId(),this.specific_genre, this.artist, this.album, Main.session);
			Main.display.asyncExec(new Runnable() {
				@Override
				public void run() {
					if(result != 0){
						System.out.println(result);
						Utils.regularCursor(Main.display, Main.shell);
						Utils.popUpBox("Success", "Song added", Main.shell);
					}
					else{
						Utils.regularCursor(Main.display, Main.shell);
						Utils.popUpBox("Failure", "Couldn't add song", Main.shell);
					}
				}
			});
		} catch (SQLException e) {
			Main.display.asyncExec(new Runnable() {
				@Override
				public void run() {
					Utils.regularCursor(Main.display, Main.shell);
					Utils.popUpFailure(Main.shell);
				}
			});
		}
	}
	
}
